﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace FeaturePlusInWeb.Control
{
    public partial class ChangeStaticBlockTempleteData : ChildWindow
    {
        private ChangeStaticBlockTempleteData()
        {
            InitializeComponent();
        }

        protected int Fid
        {
            get;
            set;
        }

        public ChangeStaticBlockTempleteData(int id):this()
        {
            Fid = id;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            var templet = new FeaturePlusInWeb.Model.FeaturedPlusTemplet();

            templet.Id = Fid;
            templet.Htmlshow = HttpUtility.HtmlEncode(this.textBox1.Text);

            var data = Newtonsoft.Json.JsonConvert.SerializeObject(templet);

            string url = Config.UrlHead + "/featuredplus/upstaticblock.php?aa" + Config.UrlEnd;

            WebClient cep = new WebClient();
            cep.UploadStringCompleted += new UploadStringCompletedEventHandler(cep_UploadStringCompleted);
            cep.UploadStringAsync(new Uri(url), "POST", data);
        }

        void cep_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Result != null && e.Result != "")
            {
                if (e.Result == "ok")
                {
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show(e.Result);
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            UploadImage um = new UploadImage();
            um.Closed += new EventHandler(um_Closed);
            um.Show();
        }

        void um_Closed(object sender, EventArgs e)
        {
            var um = (UploadImage)sender;

            if (um.DialogResult == true)
            {
                this.textBox2.Text = um.ImgUrl;
            }
        }
    }
}

